﻿/*
 * Copyright (c) 2011 Shawn Rakowski
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to 
 * deal in the Software without restriction, including without limitation the 
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 
 * sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 * SOFTWARE. 
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using ROS.Persistence.DAO.DTO;

namespace ROS.Persistence.DAO.MySQLDAO
{
    internal class OrderItemDAO : MySQLDAO, IOrderItemDAO
    {
        #region Fields

        private MySQLDAOCmdExecuter<OrderItemDTO> cmdExec;

        #endregion

        #region Contructor

        public OrderItemDAO()
        {
            cmdExec = new MySQLDAOCmdExecuter<OrderItemDTO>(this);
        }

        #endregion

        #region Methods

        private const string insertSQL =
            "INSERT INTO order_item_tbl (order_id, quantity, order_item_pricing_id, menu_item_id) " +
            "VALUES (@order_id, @quantity, @order_item_pricing_id, @menu_item_id)";
        public void Insert(OrderItemDTO dto)
        {
            dto.ID = cmdExec.PerformInsertNonQuery(insertSQL, loadParametersFromDTO(dto));
        }

        private const string selectByIDSQL =
            "SELECT * FROM order_item_tbl WHERE order_item_id = @order_item_id";
        public OrderItemDTO SelectByID(long id)
        {
            Dictionary<String, object> parameters = new Dictionary<string, object>();
            parameters["@order_item_id"] = id;

            MySQLDAOCmdExecuter<OrderItemDTO>.LoadDTODelegate loadOrderItemDTO =
                new MySQLDAOCmdExecuter<OrderItemDTO>.LoadDTODelegate(loadDTOFromReader);
            OrderItemDTO OrderItemDTO = 
                (OrderItemDTO)cmdExec.PerformSingletonSelectQuery(selectByIDSQL, parameters, loadOrderItemDTO);            

            return OrderItemDTO;
        }

        const string selectAllSQL = 
            "SELECT * FROM order_item_tbl";
        public IList<OrderItemDTO> SelectAll()
        {
            MySQLDAOCmdExecuter<OrderItemDTO>.LoadDTODelegate loadOrderItemDTO =
                new MySQLDAOCmdExecuter<OrderItemDTO>.LoadDTODelegate(loadDTOFromReader);
            return (IList<OrderItemDTO>)cmdExec.PerformSelectQuery(selectAllSQL, null, loadOrderItemDTO);       
        }
      
        private const string updateSQL =
            "UPDATE order_item_tbl SET quantity = @quantity, order_item_pricing_id = @order_item_pricing_id " +
            "WHERE order_item_id = @order_item_id";
        public void Update(OrderItemDTO dto)
        {
            cmdExec.PerformNonQuery(updateSQL, loadParametersFromDTO(dto));
        }

        const string deleteByIDSQL = 
            "DELETE FROM order_item_tbl WHERE order_item_id = @order_item_id";
        public void DeleteByID(long id)
        {
            Dictionary<String, object> parameters = new Dictionary<string, object>();
            parameters["@order_item_id"] = id;
            cmdExec.PerformNonQuery(deleteByIDSQL, parameters);
        }

        private Dictionary<String, object> loadParametersFromDTO(OrderItemDTO dto)
        {
            Dictionary<String, object> parameters = new Dictionary<string, object>();
            parameters["@order_item_id"] = dto.ID;
            parameters["@order_id"] = dto.OrderID;
            parameters["@quantity"] = dto.Quantity;
            parameters["@order_item_pricing_id"] = dto.OrderItemPricingID;
            parameters["@menu_item_id"] = dto.MenuItemID;
            return parameters;
        }

        private OrderItemDTO loadDTOFromReader(MySqlDataReader dataReader)
        {
            OrderItemDTO dto = new OrderItemDTO();
            dto.ID = dataReader.GetInt64("order_item_id");
            dto.OrderID = dataReader.GetInt64("order_id");
            dto.Quantity = dataReader.GetInt32("quantity");
            dto.OrderItemPricingID = dataReader.GetInt64("order_item_pricing_id");
            dto.MenuItemID = dataReader.GetInt64("menu_item_id");
            return dto;
        }

        #endregion
    }
}
